package tree;

/**
 * 遍历二叉树的三种方式
 */
public class TreeIterator {

  /**
   * 先序遍历
   */
  public void preOrder(TreeNode root) {
    if (root == null) {
      return;
    }
    System.out.print(root.getValue() + " ");
    preOrder(root.getLeft());
    preOrder(root.getRight());
  }

  /**
   * 中序遍历
   */
  public void inOrder(TreeNode root) {
    if (root == null) {
      return;
    }
    inOrder(root.getLeft());
    System.out.print(root.getValue() + " ");
    inOrder(root.getRight());
  }

  /**
   * 后序遍历
   */
  public void postOrder(TreeNode root) {
    if (root == null) {
      return;
    }
    postOrder(root.getLeft());
    postOrder(root.getRight());
    System.out.print(root.getValue() + " ");
  }


  public static void main(String args[]) {
    SimpleTreeCreator simpleTreeCreator = new SimpleTreeCreator();
    TreeNode root = simpleTreeCreator.createSampleTree();
    TreeIterator treeIterator = new TreeIterator();
    System.out.print("先序遍历的结果");
    treeIterator.preOrder(root);
    System.out.println();
    System.out.print("中序遍历的结果");
    treeIterator.inOrder(root);
    System.out.println();
    System.out.print("后序遍历的结果");
    treeIterator.postOrder(root);
  }


}
